﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace ImageFan.ImageHandling
{
    [DebuggerDisplay("{_folderNameWithPath}")]
    internal class ImageFolder
    {
        public ImageFolder(string folderName)
        {
            if (string.IsNullOrEmpty(folderName))
                throw new ArgumentException("Folder name cannot be empty.",
                                            "folderName");

            _folderNameWithPath = Path.GetFullPath(folderName);
        }

        public IList<ImageFile> GetImageFiles()
        {
            var imageFiles = new List<ImageFile>();

            FileInfo[] filesInformation = null;
            try
            {
                filesInformation = new DirectoryInfo(_folderNameWithPath)
                                            .GetFiles("*", SearchOption.TopDirectoryOnly);
            }
            catch
            {
            }

            if ((filesInformation != null) && (filesInformation.Length > 0))
            {
                Array.Sort(filesInformation,
                           (file1, file2) => file1.Name.CompareTo(file2.Name));
                
                foreach (var aFileInfo in filesInformation)
                    switch (aFileInfo.Extension.ToLower())
                    {
                        case ".bmp":
                        case ".jpg":
                        case ".jpe":
                        case ".jpeg":
                        case ".gif":
                        case ".tif":
                        case ".tiff":
                        case ".png":
                            imageFiles.Add(new ImageFile(aFileInfo.FullName));
                            break;
                    }
            }

            return imageFiles;
        }


        #region Private

        private string _folderNameWithPath;

        #endregion
    }
}
